device.wakeUpIfNeeded(); //唤醒设备
device.keepScreenOn(); //保持亮屏
toastLog("已唤醒");
sleep(500);
unlock_screen();//只支持安卓>=7.0
stop_app();
is_login();
in_kaoqin();
do_clock_in();
device.cancelKeepingAwake();
toastLog("主程序执行完毕，取消设备常亮");


//子程序：
//1.亮屏//    bright_screen()
//2.解锁//unlock_screen()
//3.结束钉钉//stop_app()
//4.是否登录//is_login()
//5.进入考勤页面//in_kaoqin()
//6.上班打卡//do_clock_in()
//7.下班打卡//do_clock_out()


//1.亮屏
function bright_screen() {
    toastLog("进入亮屏子程序");
    device.wakeUpIfNeeded(); //唤醒设备
    device.keepScreenOn(); //保持亮屏
    toastLog("已唤醒");
    if (!device.isScreenOn()) {
        toastLog("未唤醒");
        device.wakeUpIfNeeded();
        bright_screen();
    }
    sleep(500);
    unlock_screen();//只支持安卓>=7.0
}

//2.解锁--安卓7.0及以上才能支持手势及坐标操作
function unlock_screen() {
    toastLog("进入解锁子程序");
    swipe(500, 1800, 500, 1000, 210); //上划调出手势界面
    var password = "123456";  //这里输入你手机的密码
    sleep(500);
    for (var i = 0; i < password.length; i++) {
        var p = text(password[i].toString()).findOne().bounds();
        click(p.centerX(), p.centerY());
        sleep(100);
    }
    sleep(500);
    home();
    sleep(500);
}

//3.结束钉钉进程，确保启动钉钉后进入主页面
function stop_app() {
    toastLog("进入结束钉钉进程子程序");
    app.openAppSetting("com.alibaba.android.rimet"); //进入钉钉设置
    text(app.getAppName("com.alibaba.android.rimet")).waitFor();
    let is_sure = textMatches(/(强制.*|.*停止|强行.*)/).clickable(true).findOne();
    if (is_sure.enabled()) {
        sleep(500);
        textMatches(/(强制.*|.*停止|.*确定.*)/).clickable(true).findOne().click();
        sleep(500);
        textMatches(/(强制.*|.*停止|.*确定.*)/).clickable(true).findOne().click();
        toastLog(app.getAppName("com.alibaba.android.rimet") + "应用已被关闭");
        sleep(500);
        home();
    } else {
        toastLog(app.getAppName("com.alibaba.android.rimet") + "应用不能被正常关闭,重试");
        sleep(500);
        home();
    }
}

//4.判断是否未登录   部分需要手动修改代码
function is_login() {
    toastLog("进入登录判定子程序");
    home();
    app.launchPackage("com.alibaba.android.rimet");
    sleep(5000);
    //判定是否在登录页面
    if (id("et_pwd_login").exists()) {
        var phone = id("et_phone_input").findOne();
        phone.setText("15530192694");
        var pwd = id("et_pwd_login").findOne();
        sleep(500);
        pwd.setText("ss970214");
        id("cb_privacy").findOne().click();
        id("btn_next").findOne().click();
        info = "账号未登录>>已登录成功";
        toastLog(info);
        sleep(5000);

    } else {
        if (className("android.widget.RelativeLayout").exists()) {
            toastLog("账号已登录");
            sleep(5000);
        } else {
            toastLog("未检测到钉钉活动页面>>重启钉钉");
            is_login();
        }
    }
}

//5.进入考勤页面
function in_kaoqin() {
    toastLog("进入考勤页面切换子程序");
    var anniu_gongzou = textContains("工作台").findOne(3000);
    if (null != anniu_gongzou) {
        toastLog("text中找到工作台按钮");
        sleep(500);
        click(anniu_gongzou.bounds().centerX(), anniu_gongzou.bounds().centerY());
        sleep(6000);
        var anniu_kaoqin = textMatches("考勤打卡").findOne(3000);
        if (null != anniu_kaoqin) {
            toastLog("text中找到考勤打卡按钮");
            sleep(500);
            click(anniu_kaoqin.bounds().centerX(), anniu_kaoqin.bounds().centerY());
            sleep(5000);
            toastLog("进入打卡页面");
        } else {
            sleep(500);
            toastLog("未进入打卡页面");
            sleep(500);
            in_kaoqin();
        }
    }
}

//6.上班打卡
function do_clock_in() {
    toastLog("进入上班打卡子程序");
    var anniu_click_in = textMatches("上班打卡").findOne(1000);
    if (null != anniu_click_in) {
        toastLog("text中找到上班打卡按钮");
        sleep(1000);
        textContains("已进入考勤范围").waitFor();
        // var randomtime = random(0, 10) * 60 * 1000;
        // toastLog(randomtime / 1000 + "秒后执行");
        click(anniu_click_in.bounds().centerX(), anniu_click_in.bounds().centerY());
        textContains("我知道了").waitFor();
        sleep(1000);
        if (null != descMatches("我知道了").findOne(1000)) {
            desc("我知道了").findOne().click();
        }
        if (null != textMatches("我知道了").findOne(1000)) {
            text("我知道了").findOne().click();
        }
        sleep(2000);
        toastLog("按下打卡按钮，打卡成功");
    } else {
        toastLog("未找到上班打卡按钮>>继续找");
        sleep(500);
        let daka_ok = textMatches(/(.*下班打.*|下班打.*)/).findOnce();
        if (daka_ok != null) {
            toastLog("已出现下班打卡按钮，无需再按");
            sleep(1000);
            home();
        } else {
            toastLog("未打卡，重试");
            do_clock_in();
        }
    }
    home();
    sleep(1000);
    toastLog("脚本已执行完毕");
}

//7.下班打卡
function do_clock_out() {
    toastLog("进入下班打卡子程序");
    var anniu_click_out = textMatches("下班打卡").findOne(3000);
    if (null != anniu_click_out) {
        toastLog("desc中找到下班打卡按钮");
        sleep(1000);
        textContains("已进入考勤范围").waitFor();
        click(anniu_click_out.bounds().centerX(), anniu_click_out.bounds().centerY());
        sleep(1000);
        if (null != descMatches("我知道了").findOne(1000)) {
            desc("我知道了").findOne().click();
        }
        if (null != textMatches("我知道了").findOne(1000)) {
            text("我知道了").findOne().click();
        }
        sleep(2000);
        toastLog("按下打卡按钮，打卡成功");
    } else {
        toastLog("未找到下班打卡按钮>>继续找");
        sleep(500);
        let daka_ok = textMatches(/(.*新打卡.*|更新打.*)/).findOnce();
        if (daka_ok != null) {
            toastLog("已出现更新打卡按钮，打卡成功");
            sleep(1000);
            home();
        } else {
            toastLog("未打卡，重试");
            do_clock_out();
        }
    }
    home();
    sleep(1000);
    toastLog("脚本已执行完毕");
}


function random(min, max) {

    return Math.floor(Math.random() * (max - min)) + min;

}
